package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.FacultyDao;
import cn.edu.sdjzu.xg.kcsj.domain.Faculty;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class FacultyService {
    private static FacultyDao facultyDao = FacultyDao.getInstance();
    private static FacultyService facultyService = new FacultyService();

    public static FacultyService getInstance(){
        return facultyService;
    }

    // 查找全部院系
    public Collection<Faculty> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Faculty> faculties = new TreeSet<Faculty>();
        try {
            faculties = facultyDao.findAll(conditionList,pagination,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculties;
    }

    //获得全部学院
    public Collection<Faculty> findAllSchool() throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Faculty> faculties = new TreeSet<Faculty>();
        try {
            faculties = facultyDao.findAllSchool(connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculties;
    }
    //根据学院id获取下属所有专业
    public Collection<Faculty> findAllDeptBySchoolId(int id)throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Faculty> faculties = new TreeSet<Faculty>();
        try {
            faculties = facultyDao.findAllDeptBySchoolId(connection,id);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculties;
    }

    //按no查找院系
    public Faculty findByNo(String no) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Faculty faculty = null;
        try {
            faculty = facultyDao.findByNo(no,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculty;
    }

    // 按id查找院系
    public Faculty find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Faculty faculty = null;
        try {
            faculty = facultyDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculty;
    }

    // 更新一条
    public boolean update(Faculty faculty) throws SQLException ,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean updated =false;
        try {
            updated = facultyDao.update(faculty,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Faculty faculty) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added =false;
        try {
            //增加
            added = facultyDao.add(faculty,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();

        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }


    // 删除
    public boolean delete(Integer id) throws SQLException,BysjException {
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            // 如果有子节点
            if(facultyDao.find(id,connection).isHaveChild()){
                throw new BysjException("存在下属系，不能删除！");
            }else {
                deleted = facultyDao.delete(id, connection);
                //提交事务
                connection.commit();
            }
        } catch (SQLException e) {
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public Boolean deleteBatch(Collection<Faculty> faculties) throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted = false;
        try {
            for (Faculty faculty:faculties){
                // 如果有子节点
                if(faculty.isHaveChild()){
                    throw new BysjException("存在下属系，不能删除！");
                }else {
                    deleted = facultyDao.delete(faculty.getId(),connection);
                    //提交事务
                    connection.commit();
                }
            }
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }


    public Collection<Faculty> findAllWithoutElse() throws SQLException,BysjException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Collection<Faculty> faculties = new TreeSet<Faculty>();
        try {
            faculties = facultyDao.findAllWithoutElse(connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return faculties;
    }
}
